package cn.itrip.trade.service.impl;

import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripTradeEnds;
import cn.itrip.common.RedisUtil;
import cn.itrip.dao.ItripHotelOrderMapper;
import cn.itrip.dao.ItripHotelTempStoreMapper;
import cn.itrip.dao.ItripTradeEndsMapper;
import cn.itrip.trade.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;
    @Autowired
    private ItripTradeEndsMapper insertItripTradeEnds;

    @Override
    public ItripHotelOrder loadItripHotelOrder(String orderNo) {
        ItripHotelOrder orders = null;
        if (redisUtil.hasKey("orderNo:" + orderNo)) {
            orders = (ItripHotelOrder) redisUtil.get("orderNo:" + orderNo);
        } else {
            orders = itripHotelOrderMapper.getorderByorderNo(orderNo);
            redisUtil.set("orderNo:" + orderNo, orders);
        }
        return orders;
    }

    /**
     * 判断该订单是否已被处理过（被更新为已支付状态）
     */
    @Override
    public boolean validation(String orderNo) {
        ItripHotelOrder orders = this.loadItripHotelOrder(orderNo);
        if (orders != null) {
            if (orders.getOrderStatus().equals(2)) {
                return true;
            }
        }
        return false;

    }

    /**
     * 支付成功
     *
     * @param orderNo 订单编号
     */
    @Override
    public void paySuccess(String orderNo, int payType, String tradeNo) {
        //更新订单状态、支付宝交易号
        ItripHotelOrder orders = this.loadItripHotelOrder(orderNo);
        orders.setOrderStatus(2);//支付成功
        orders.setPayType(payType);
        orders.setTradeNo(tradeNo);//交易号（如支付宝交易号）
        int num = itripHotelOrderMapper.updateByOrderId(orders);
        if (redisUtil.hasPreKey("order:*")) {
            redisUtil.deletePrefix("order:*");
        }
        ItripTradeEnds itripTradeEnds = new ItripTradeEnds();
        itripTradeEnds.setId(orders.getId());
        itripTradeEnds.setOrderNo(orders.getOrderNo());
        insertItripTradeEnds.insert(itripTradeEnds);
        Long roomid = orders.getRoomId();
        Long hotelid = orders.getHotelId();
        int count = orders.getCount();
        Long modifiedby = orders.getCreatedBy();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String modifiedDate = format.format(new Date());
        itripHotelTempStoreMapper.updatestore(modifiedby, count, hotelid, roomid);
        if (redisUtil.hasPreKey("tempStore:*")) {
            redisUtil.deletePrefix("tempStore:*");
        }
    }

    /**
     * 支付失败
     **/
    @Override
    public void payFailed(String orderNo, int payType, String tradeNo) {
        ItripHotelOrder orders = this.loadItripHotelOrder(orderNo);
        orders.setOrderStatus(1);//支付状态：已取消
        orders.setPayType(payType);
        orders.setTradeNo(tradeNo);//交易号（如支付宝交易号）
        itripHotelOrderMapper.updateByOrderId(orders);
        if (redisUtil.hasPreKey("order:*")) {
            redisUtil.deletePrefix("order:*");
        }
    }


    @Override
    public boolean processed(String orderNo) throws Exception {
        // TODO Auto-generated method stub
        ItripHotelOrder itripHotelOrder = this.loadItripHotelOrder(orderNo);
        return itripHotelOrder.getOrderStatus().equals(2) && itripHotelOrder.getTradeNo() != null;
    }

}
